﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;

public partial class Admin_admin_sanpham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            calngaycapnhat.SelectedDate = DateTime.Today;
            Load_data();
        }
    }
    //=============================================================================================
    protected void Load_data()
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection cn = new SqlConnection(con);
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        cn.Open();
        string str = "select * from SanPham";
        SqlCommand cmd = new SqlCommand(str, cn);
        cmd.CommandText = str;
        cmd.Connection = cn;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
    protected void btluuspmoi_Click(object sender, EventArgs e)
    {
        string Hinh = FUlhinhanh.FileName.ToString();
        DataTable dthinh = XLDL.Docbang("select hinh from SanPham where hinh='" + Hinh + "'");
        if (!FUlhinhanh.HasFile)
        {
            lbthongbao.Text = "Bạn vui lòng chọn hình minh họa cho sản phẩm";
        }
        else if (dthinh.Rows.Count != 0)
        {
            lbthongbao.Text = "---Trùng tên hình--- Hình minh họa sản phẩm này đã có rồi";
        }
        else
        {
            int MaDM = int.Parse(ddldanhmuc.SelectedItem.Value);
            string TenSP = txttensp.Text;
            string MoTa = txtmota.Text;
            string Ngaycapnhat = calngaycapnhat.SelectedDate.ToString();
            int Soluong = int.Parse(txtsoluongban.Text);
            string Donvitinh = txtdonvitinh.Text;
            int Dongia = int.Parse(txtdongia.Text);
            FUlhinhanh.SaveAs(MapPath("~/hinhsanpham/" + Hinh));
            string str = "INSERT INTO SanPham(madm,tensp,mota,ngaycapnhat,soluong,donvitinh,dongia,hinh) VALUES(" + MaDM + ",N'" + TenSP + "',N'" + MoTa + "',N'" + Ngaycapnhat + "','" + Soluong + "',N'" + Donvitinh + "','" + Dongia + "','" + Hinh + "')";
            XLDL.thuchienlenh(str);
            Response.Redirect("~/Admin/adminsanpham.aspx");

        }
    }
   
    protected void rept_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection cn = new SqlConnection(con);
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        cn.Open();
        if (e.CommandName == "Sua")
        {
            ((Label)e.Item.FindControl("lbltensp")).Visible = false;
            ((Label)e.Item.FindControl("lblmota")).Visible = false;
            ((Label)e.Item.FindControl("lbldongia")).Visible = false;
            ((Label)e.Item.FindControl("lbltrangthai")).Visible = false;
            ((Label)e.Item.FindControl("lblhinh")).Visible = false;
            ((Label)e.Item.FindControl("lblmadm")).Visible = false;
            ((Label)e.Item.FindControl("lblsoluong")).Visible = false;
            ((Label)e.Item.FindControl("lbldonvitinh")).Visible = false;
            ((Label)e.Item.FindControl("lblngaycapnhat")).Visible = false;
            ((TextBox)e.Item.FindControl("txtTensp")).Visible = true;
            ((TextBox)e.Item.FindControl("txtmota")).Visible = true;
            ((TextBox)e.Item.FindControl("txtdongia")).Visible = true;
            ((TextBox)e.Item.FindControl("txttrangthai")).Visible = true;
            ((TextBox)e.Item.FindControl("txthinh")).Visible = true;
            ((TextBox)e.Item.FindControl("txtmadm")).Visible = true;
            ((TextBox)e.Item.FindControl("txtsoluong")).Visible = true;
            ((TextBox)e.Item.FindControl("txtdonvitinh")).Visible = true;
            ((TextBox)e.Item.FindControl("txtngaycapnhat")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkSua")).Visible = false;
            ((LinkButton)e.Item.FindControl("Button1")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkLuu")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkDung")).Visible = true;
        }
        if (e.CommandName == "Luu")
        {
            string tensp = ((TextBox)e.Item.FindControl("txtTensp")).Text;
            string mota = ((TextBox)e.Item.FindControl("txtmota")).Text;
            string dongia = ((TextBox)e.Item.FindControl("txtdongia")).Text;
            string trangthai = ((TextBox)e.Item.FindControl("txttrangthai")).Text;
            string hinh = ((TextBox)e.Item.FindControl("txthinh")).Text;
            string madm = ((TextBox)e.Item.FindControl("txtmadm")).Text;
            string soluong = ((TextBox)e.Item.FindControl("txtsoluong")).Text;
            string donvitinh = ((TextBox)e.Item.FindControl("txtdonvitinh")).Text;
            string ngaycapnhat = ((TextBox)e.Item.FindControl("txtngaycapnhat")).Text;
            string sql = "Update SanPham set tensp= @tensp, mota=@mota, dongia=@dongia, trangthai=@trangthai, hinh=@hinh ,madm=@madm, soluong=@soluong, donvitinh=@donvitinh, ngaycapnhat=@ngaycapnhat where masp = @masp";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandText = sql;
            cmd.Connection = cn;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            //SqlDataAdapter adp = new SqlDataAdapter("Update NhanVien set TenNhanVien= @TenNhanVien, NgaySinh=@NgaySinh,Email=@Email,DienThoai=@DienThoai ,Password=@Password where IDNhanVien = @IDNhanVien", cn);
            adp.SelectCommand.Parameters.AddWithValue("@tensp", tensp);
            adp.SelectCommand.Parameters.AddWithValue("@mota", mota);
            adp.SelectCommand.Parameters.AddWithValue("@dongia", dongia);
            adp.SelectCommand.Parameters.AddWithValue("@trangthai", trangthai);
            adp.SelectCommand.Parameters.AddWithValue("@hinh", hinh);
            adp.SelectCommand.Parameters.AddWithValue("@madm", madm);
            adp.SelectCommand.Parameters.AddWithValue("@soluong", soluong);
            adp.SelectCommand.Parameters.AddWithValue("@donvitinh", donvitinh);
            adp.SelectCommand.Parameters.AddWithValue("@ngaycapnhat", ngaycapnhat);
            adp.SelectCommand.Parameters.AddWithValue("@masp", e.CommandArgument);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Load_data();
        }
        if (e.CommandName == "Dung")
        {
            ((Label)e.Item.FindControl("lbltensp")).Visible = true;
            ((Label)e.Item.FindControl("lblmota")).Visible = true;
            ((Label)e.Item.FindControl("lbldongia")).Visible = true;
            ((Label)e.Item.FindControl("lbltrangthai")).Visible = true;
            ((Label)e.Item.FindControl("lblhinh")).Visible = true;
            ((Label)e.Item.FindControl("lblmadm")).Visible = true;
            ((Label)e.Item.FindControl("lblsoluong")).Visible = true;
            ((Label)e.Item.FindControl("lbldonvitinh")).Visible = true;
            ((Label)e.Item.FindControl("lblngaycapnhap")).Visible = true;
            ((TextBox)e.Item.FindControl("txtTensp")).Visible = false;
            ((TextBox)e.Item.FindControl("txtmota")).Visible = false;
            ((TextBox)e.Item.FindControl("txtdongia")).Visible = false;
            ((TextBox)e.Item.FindControl("txttrangthai")).Visible = false;
            ((TextBox)e.Item.FindControl("txthinh")).Visible = false;
            ((TextBox)e.Item.FindControl("txtmadm")).Visible = false;
            ((TextBox)e.Item.FindControl("txtsoluong")).Visible = false;
            ((TextBox)e.Item.FindControl("txtdonvitinh")).Visible = false;
            ((TextBox)e.Item.FindControl("txtngaycapnhap")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkSua")).Visible = true;
            ((LinkButton)e.Item.FindControl("Button1")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkLuu")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkDung")).Visible = false;
        }
        if (e.CommandName == "Xoa")
        {
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            string sql = "delete from SanPham where masp=@masp";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Parameters.AddWithValue("@masp", e.CommandArgument);// dung hàm  CommandArgument moi lay dc con truyen la k lay dc
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Load_data();
        }
        cn.Close();
    }
}
	
	
	